package longrise.phone.com.bjjt_henan.bindinginformation;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;
import com.longrise.android.listview_library_refrush.PullToRefreshBase;
import com.longrise.android.listview_library_refrush.PullToRefreshListView;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.UiUtil;

import longrise.phone.com.bjjt_henan.Adapter.SnapshotAdapter;
import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;

/**
 * 我的随手拍界面
 */

public class MySnapshotView extends LFView {

    private Context mContext;

    private View mView;

    private PullToRefreshListView mPListView;

    private SnapshotAdapter mAdapter;

    private BaseApplication mApplication;

    private View emptyView;//无数据时的背景

    public MySnapshotView(Context context) {
        super(context);
        this.mContext = context;
        this.mApplication = ((MainActivity) context).getAppData();
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter form = new FormParameter();
        form.setShowtitle(false);
        return form;
    }

    @Override
    public View getView() {
        return mView;
    }

    @Override
    public void init() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_snapshot, null);
        if (null == mView) {
            return;
        }
        //标题栏
        TextView titleTV = (TextView) mView.findViewById(R.id.tv_title);
        titleTV.setText("我的随手拍");
        mView.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnDestroy();
                closeForm();
            }
        });
        mPListView = (PullToRefreshListView) mView.findViewById(R.id.snapshot_rlv);
        mPListView.setMode(PullToRefreshBase.Mode.DISABLED);

        mAdapter = new SnapshotAdapter(mContext);
        mAdapter.setListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EntityBean bean = (EntityBean) v.getTag();
                if (bean == null) {
                    return;
                }
                SnapshotDetailView detailView = new SnapshotDetailView(mContext, bean);
                FrameworkManager.getInstance().showNewForm(mContext, detailView);
            }
        });
        mPListView.setAdapter(mAdapter);

        emptyView = mView.findViewById(R.id.empty_tv);
    }

    @Override
    public void refresh() {
        String uid = mApplication.getKckpName();
        String token = mApplication.getToken();
        EntityBean baseBean = mApplication.getLoginInfo().getBean("userinfo");
        String phone = baseBean.getString("mobilephone");
        DialogUtils.showDialog(mContext);
        ApiImpI.getInstance(mContext.getApplicationContext()).searchSnapshot(phone, uid, token, new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
                DialogUtils.closeDialog();
                if (!TextUtils.isEmpty(error)) {
                    UiUtil.showToast(mContext, error);
                }
            }

            @Override
            public void onSuccess(Object result) {
                DialogUtils.closeDialog();
                if (mView == null) {
                    return;
                }
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                if (TextUtils.equals(restate, "1")) {
                    EntityBean dataBean = bean.getBean("data");
                    if (null != dataBean) {
                        EntityBean[] beans = dataBean.getBeans("inclist");
                        if (beans == null || beans.length < 1) {
                            mPListView.setEmptyView(emptyView);
                        } else {
                            emptyView.setVisibility(View.GONE);
                        }
                        if (null != mAdapter) {
                            mAdapter.setData(beans);
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                }
            }
        });
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        mView = null;
        if (null != mPListView) {
            mPListView.setAdapter(null);
            mPListView = null;
        }
        mAdapter = null;
    }
}
